package com.leetcode.LC;

public class LC605 {
    public boolean canPlaceFlowers(int[] flowerbed, int n) {

        if (n < 1) {
            return true;
        }
        if (flowerbed.length==1&&flowerbed[0]==0&&n==1){
            return true;
        }else if (flowerbed.length==1){
            return false;
        }
        int count = 0;
        for (int i = 0; i < flowerbed.length; i++) {
            if (i - 1 >= 0 && i + 1 <= flowerbed.length - 1 && flowerbed[i - 1] == 0 && flowerbed[i] == 0 && flowerbed[i + 1] == 0) {
                flowerbed[i] = 1;
                count++;
                if (count >= n) {
                    return true;
                }
            } else if (i - 1 >= 0 && i == flowerbed.length - 1 && flowerbed[i - 1] == 0 && flowerbed[i] == 0) {
                flowerbed[i] = 1;
                count++;
                if (count >= n) {
                    return true;
                }
            } else if (i + 1 <= flowerbed.length - 1 && i == 0 && flowerbed[i + 1] == 0 && flowerbed[i] == 0) {
                flowerbed[i] = 1;
                count++;
                if (count >= n) {
                    return true;
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        System.out.println(new LC605().canPlaceFlowers(new int[]{1, 0, 0, 0, 0}, 3));

    }
}
